import api from "@/data/api/index.js";
import role from "@/router/index";
import qs from "qs";

const action = {
    tab(tree, num) {
        tree.commit('tab', num)
    },
    getMenus({ commit }) {

        //这个是新增的action
        return new Promise((resolve, reject) => {
            let data = {
                jsonStr: JSON.stringify(role.options.routes)

            };
            api.menuroleidbeTwo(qs.stringify(data))
                .then(res => {
                    //这里的getMenus是调用request方法从服务端获得路由菜单数据的Promise，类似getInfo
                    let menus = res.map(function (item) {
                          return item.path;
                    });

                    commit("SET_MENUS", menus);
                    resolve(menus);
                })
                .catch(error => {
                    reject(error);
                });
        });
    },
    // 存储用户信息
    getInfo({ commit }) {
        return new Promise((resolve, reject) => {
            api.userInfo().then(res => {
                if (res.data.code == 0) {
                    commit("ADD_USER", res.data);
                    commit("SET_NAME", res.data.username);
                    resolve(res.data);
                }
            }).catch(error => {
                reject(error);
            });
        });
    },


}
export default action